package top.huntwolf.admin.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.huntwolf.admin.common.enums.CodeMsg;
import top.huntwolf.admin.dao.TopicalDao;
import top.huntwolf.admin.exception.ServiceException;
import top.huntwolf.admin.pojo.book.BuyBook;
import top.huntwolf.admin.pojo.topical.Topical;
import top.huntwolf.admin.pojo.topical.TopicalBook;
import top.huntwolf.admin.pojo.topical.TopicalBookList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TopicalService {


    @Autowired
    TopicalDao topicalDao;


    public void add(Topical topical) {
        topicalDao.add(topical);
    }

    public void update(Topical topical) {
        topicalDao.update(topical);
    }

    public void delete(Integer tid) {
        topicalDao.delete(tid);
    }

    //    public Map<String, List<TopicalBook>> getByTopical() {
    public List<TopicalBookList> getByTopical() {
//        Map<String, List<TopicalBook>> map =  topicalDao.getByTopical();
        // 定义一个大的map，根据专题名字，来返回专题的所有内容
//        Map<String, List<TopicalBook>> map = new HashMap<>();
        List<TopicalBookList> returnList = new ArrayList<>();
        // 获取专题名字
        List<TopicalBookList> nameList = topicalDao.getTopicalName();
        if (nameList.size() == 0) {
            throw new ServiceException(CodeMsg.NOT_FIND_TOPICAL);
        }
        // 根据nameList中的名字查找特定的专题list

        for (TopicalBookList tbl : nameList) {
            TopicalBookList ele = new TopicalBookList();
            ele.name = tbl.name;
            ele.imgUrl = tbl.imgUrl;
            ele.introduce = tbl.introduce;
            List<TopicalBook> list = topicalDao.getTopicalByName(tbl.name);
            for (TopicalBook book : list) {
                StringToHashMap(book);
            }
            ele.topicalBooks = list;
            returnList.add(ele);
        }

        return returnList;
    }


    // 精选清单查询
//    public Map<String, List<TopicalBook>> getSelectedList() {
    public   List<TopicalBookList> getSelectedList() {

        // 1、 先创建一个map来封装集合
//        Map<String, List<TopicalBook>> map = new HashMap<>();
        List<TopicalBookList> returnList = new ArrayList<>();

        // 2、获取精选清单的名字列表
        List<TopicalBookList> nameList = topicalDao.getBySelectedListNames();

        // 3、根据精选清单名字列表找对应的书集
        for (TopicalBookList tbl : nameList) {
            // 定义一个元素，封装该元素，最后加入到returnList中去
            TopicalBookList ele = new TopicalBookList();
            ele.name = tbl.name;
            ele.imgUrl = tbl.imgUrl;
            ele.introduce = tbl.introduce;
            List<TopicalBook> list = topicalDao.getBySelectedListByName(ele.name);

            for (TopicalBook book : list) {
                StringToHashMap(book);
            }
//            map.put(name, list);
            ele.topicalBooks = list;
            returnList.add(ele);
        }


        if (returnList.size() == 0) {
            throw new ServiceException(CodeMsg.NOT_FIND_TOPICAL);
        }
        return returnList;
    }

    public List<TopicalBook> getHotTropical() {
        List<TopicalBook> list = topicalDao.getHot();
        for (TopicalBook book : list) {
            StringToHashMap(book);
        }
        if (list.size() == 0) {
            throw new ServiceException(CodeMsg.NOT_FIND_TOPICAL);
        }
        return list;
    }

    private void StringToHashMap(TopicalBook book) {

        String stock = book.getBookCondition();
        String[] conditions = stock.split(",");
        book.setBookCondition(null);

        stock = book.getDiscount();
        String[] discounts = stock.split(",");
        book.setDiscount(null);

        stock = book.getStorage();
        String[] storages = stock.split(",");
        HashMap<String, String> a = new HashMap<>();
        HashMap<String, Integer> b = new HashMap<>();
        for (int i = 0; i < storages.length; i++) {
            a.put(conditions[i], discounts[i]);
            b.put(conditions[i], Integer.valueOf(storages[i]));
        }
        book.setDiscounts(a);
        book.setStorages(b);
        book.setStorage(null);
    }
}
